import { component } from "vue/types/umd";

function broadcast(componentName, eventName, params) {
    this.$children.forEach(element => {
        const name = element.$options.name;        

        if (name === componentName) {
            element.$emit.apply(element, [eventName].concat(params))
        } else {
            broadcast.apply(element, [componentName, eventName].concat([params]))
        }
    });
}


export default {
    dispatch (componentName, eventName, params) {
        let _parent = this.$parent || this.$root;
        let name = _parent.$options.name;
        while (_parent && (!name || name !== componentName)) {
            _parent = _parent.$parent

            if (_parent) {
                name = _parent.$options.name;
            }
        }
        if (_parent) {
            _parent.$emit.apply(_parent, [eventName].concat(params));
        }
    },
    broadcast(componentName, eventName, params) {
        broadcast.call(this, componentName, eventName, params)
    }
}